//登录和注册页面转换
let spanDom = document.querySelectorAll('.top>span');
let loginDom = document.querySelector('.formOne');
let registDom = document.querySelector('.formTwo');
spanDom[0].onclick = function () {
    spanDom[1].className = '';
    this.className = 'active';
    registDom.style.right = '-360px';
    loginDom.style.right = '45px';
    loginDom.style.left = '45px';
}
spanDom[1].onclick = function () {
    spanDom[0].className = '';
    this.className = 'active';
    registDom.style.right = '45px';
    loginDom.style.right = '0px';
    loginDom.style.left = '-360px';
}
//登录页面填入数据格式判断
let flag1, flag2, flag3;
let firstPhoneNumber = document.querySelector('.firstphoneNumber');
let password = document.querySelector('.psw');
let firstSure = document.querySelector('.sure>input');
let sumbit = document.querySelector('.formOne>.sub');
firstPhoneNumber.onblur = function () {
    let value = this.value;
    if (/^1[356789]\d{9}$/.test(value)) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        flag1 = true
    } else {
        layer.msg('手机号格式错误', { icon: 5 });
        flag1 = false;
    }
    check()
}
password.onblur = function () {
    let value = this.value;
    if (this.value.length >= 6) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        flag2 = true
    } else {
        layer.msg('密码不得小于六位数', { icon: 5 });
        flag2 = false;
    }
    check()
}
firstSure.onclick = function () {
    if (this.checked) {
        flag3 = true
    } else {
        flag3 = false
    }
    check()
}
function check() {
    flag1 & flag2 & flag3 ? sumbit.disabled = false : sumbit.disabled = true;
    if (flag1 & flag2 & flag3) {
        sumbit.style.opacity = '1';
    }
}

//登录页面数据提交后台后台
sumbit.onclick = function (event) {
    event.preventDefault();
    ajax({
        url: baseUrl + '/user/login',
        type: 'post',
        data: {
            mobile: firstPhoneNumber.value,
            password: password.value
        },
        success: function (response) {
            console.log(response);
            if (response.code[0] == "账号或者密码错误") {
                layer.msg('账号或者密码错误', { icon: 5 });
                return false;
            } else {
                let expires = 0;
                expires = 7 * 3600 * 24;
                let userInfo = response.data;
                setCookie('userInfo', JSON.stringify(userInfo), expires);
                location.href = './first.page.html'
            }
            firstPhoneNumber.value = '';
            firstPhoneNumber.nextElementSibling.innerHTML = ''
            password.value = '';
            password.nextElementSibling.innerHTML = ''
            firstSure.checked = false;
            sumbit.checked = false;
            sumbit.style.opacity = '0.5';
        }
    })
}
//注册页面填入数据格式判断
let res1, res2, res3, res4;
let secondPhoneNumber = document.querySelector('.secondphoneNumber');
let registpsw = document.querySelector('.registpsw>input');
let code = document.querySelector('.codeTwo');
let secondSure = document.querySelector('.secondsure>input');
let secondSumbit = document.querySelector('.formTwo>.sub');
secondPhoneNumber.onblur = function () {
    let value = this.value;
    if (/^1[356789]\d{9}$/.test(value)) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        res1 = true
    } else {
        layer.msg('手机号格式错误', { icon: 5 });
        res1 = false;
    }
    isCheck()
}
registpsw.onblur = function () {
    let value = this.value;
    if (this.value.length >= 6) {
        this.nextElementSibling.innerHTML = '√';
        this.nextElementSibling.style.color = 'green';
        res2 = true
    } else {
        layer.msg('注册密码不得小于六位数', { icon: 5 });
        res2 = false;
    }
    isCheck()
}
secondSure.onclick = function () {
    if (this.checked) {
        res3 = true
    } else {
        res3 = false
    }
    isCheck()
}
function isCheck() {
    res1 & res2 & res3 ? secondSumbit.disabled = false : secondSumbit.disabled = true;
    if (res1 & res2 & res3) {
        secondSumbit.style.opacity = '1';
    }
}
// 注册页面数据提交后台并获得验证码
document.querySelector(".getcode").onclick = function () {
    let num = 60;
    let that = this
    let timer = setInterval(function () {
        num--;
        if (num >= 0) {
            that.innerText = num + `s重新获取`;
            that.disabled = true;
        } else {
            clearInterval(timer);
            that.innerText = `重新获取`;
            that.disabled = false;
        }
    }, 1000)
    pajax({
        url: baseUrl + '/user/sendVerify',
        type: 'get',
        data: {
            mobile: secondPhoneNumber.value,
        }
    })
        .then(function (response) {
            if (!secondPhoneNumber.value) {
                alert('请输入手机号');
                clearInterval(timer);
                that.innerText = `获取验证码`;
                return
            }
            code.value = response.data.verifyCode;
            secondSumbit.onclick = function (event) {
                event.preventDefault();
                ajax({
                    url: baseUrl + '/user/register',
                    type: 'post',
                    data: {
                        mobile: secondPhoneNumber.value,
                        verifyCode: code.value,
                        password: registpsw.value
                    },
                    contentType: 'application/json',
                    success: function (response) {
                        console.log(response);
                        if (response.code == 0) {
                            spanDom[1].className = '';
                            spanDom[0].className = 'active';
                            registDom.style.right = '-360px';
                            loginDom.style.right = '45px';
                            loginDom.style.left = '45px';
                        } else if (response.code[0] == '手机号不能重复') {
                            layer.msg('请勿重复注册', { icon: 5 });
                        }
                        secondPhoneNumber.value = '';
                        secondPhoneNumber.nextElementSibling.innerHTML = '';
                        registpsw.value = '';
                        registpsw.nextElementSibling.innerHTML = '';
                        code.value = '';
                        clearInterval(timer);
                        that.innerText = `重新获取`;
                        secondSure.checked = false;
                        secondSumbit.checked = false;
                        secondSumbit.style.opacity = '0.5';
                    }
                })
            }
        })
}